package cn.hairui.blog.controller;

import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.entity.Myinfo;
import cn.hairui.blog.entity.Type;
import cn.hairui.blog.service.MyinfoService;
import com.alibaba.druid.support.json.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 我的信息表 前端控制器
 * </p>
 *
 * @author laoganbu
 * @since 2020-04-06
 */
@Controller

public class MyinfoController {

    @Autowired
    private MyinfoService myinfoService;

    @RequestMapping(value = "/manage/config")
    public String config(Model model) {
        Myinfo myinfo = myinfoService.getById(PubConstant.MY_INFO_ID);
        model.addAttribute("myinfo", myinfo);
        return "page/myinfo/config";
    }

    // 更新数据
    @RequestMapping(value = "/manage/myinfo/save")
    @ResponseBody
    @Transactional
    public String updateData(HttpServletRequest request, Myinfo myinfo) {
        Map map = new HashMap<Object, Object>();
        myinfoService.saveOrUpdate(myinfo);
        map.put(PubConstant.flag, PubConstant.success);
        return JSONUtils.toJSONString(map);
    }
}
